from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings
from apps.users.models import User
import logging
logger = logging.getLogger('django')
# 加密方法
def dumps_email_token(token):
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    data_dumps = serializer.dumps(token)
    return data_dumps.decode()


# 解密方法
def loads_email_token(token):
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    date_loads = serializer.loads(token)

    return date_loads



# 验证token方法

def cleck_token(token):
    token_dict = loads_email_token(token)
    user_id = token_dict.get('user_id')
    email = token_dict.get('emails')

    try:
        user = User.objects.get(id=user_id, email=email)

    except User.DoesNotExist:
        logger.error('没有用户')

    return token_dict

